package com.yao.club.controller.goods;

import com.yao.club.entity.request.CommentRequest;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.entity.vo.GoodsCommentVO;
import com.yao.club.service.GoodsCommentService;
import com.yao.club.token.PassToken;
import com.yao.club.token.UserToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Api(tags = "俱乐部商城-商品评论")
@RestController
@RequestMapping("/mall/goodsComment")
public class GoodsCommentController {

    @Autowired
    private GoodsCommentService goodsCommentService;
    @Resource
    HttpServletRequest request;
    @PassToken
    @ApiOperation("分页获取商品评论")
    @GetMapping("/page/{goodsId}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer")
    })
    public ResponseEntity<ResultEntity<GoodsCommentVO>> pageComments(
            @PathVariable Long goodsId,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        String username = (String) request.getAttribute("username");
        return goodsCommentService.pageComments(goodsId, pageNum, pageSize,username);
    }
    @UserToken
    @ApiOperation("发布评论")
    @PostMapping("/add")
    public ResponseEntity<Boolean> addComment(
            @RequestBody CommentRequest commentRequest) {
        String username = (String) request.getAttribute("username");
        return goodsCommentService.addComment(commentRequest,username);
    }
    @UserToken
    @ApiOperation("回复评论")
    @PostMapping("/reply")
    public ResponseEntity<Boolean> replyComment(
            @RequestBody CommentRequest commentRequest) {
        String username = (String) request.getAttribute("username");
        return goodsCommentService.replyComment(commentRequest, username);
    }
    @UserToken
    @ApiOperation("点赞/取消点赞评论")
    @PostMapping("/like/{commentId}")
    @ApiImplicitParam(name = "commentId", value = "评论ID", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<Boolean> toggleCommentLike(
            @PathVariable Long commentId) {
        String username = (String) request.getAttribute("username");
        return goodsCommentService.toggleCommentLike(commentId, username);
    }
    @UserToken
    @ApiOperation("删除评论")
    @DeleteMapping("/{commentId}")
    @ApiImplicitParam(name = "commentId", value = "评论ID", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<Boolean> deleteComment(
            @PathVariable Long commentId) {
        String username = (String) request.getAttribute("username");
        return goodsCommentService.deleteComment(commentId,username);
    }
} 